package algotradingfx.strategies;

import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

import org.apache.log4j.Logger;
import algotradingfx.data.TickWriter;
import algotradingfx.utils.Constants;
import algotradingfx.utils.FXAsset;
import atp.client.ATPRuntime;
import atp.client.trading.methods.TradingMethod.Side;
import atp.client.trading.methods.TradingMethod.Type;
import atp.client.trading.strategies.StrategyPush;
import atp.commons.util.StrategyInfo;
import atp.commons.util.Tick;

public class NegativeSpread extends StrategyPush {
	public static Logger LOGGER = Logger.getLogger(NegativeSpread.class);

	private static final long serialVersionUID = 1728280496562117242L;

	private static final String STRATEGY_NAME = "TriangleArbitrage";

	private Map<FXAsset, Tick> tickMap;

	private List<String> openTickets;
	
	public NegativeSpread(String ssID, String password) {
		super(new StrategyInfo(STRATEGY_NAME), ssID, password);
	}

	@Override
	public void initialise() {
		// create a Thread to run RDT at background all time to analyse trend
		// draw a distribution on what will happen for the rest of the day
		// 9pm-5pm
		// every 15 mins
		// find the turning point if any
		// calculate the probability of changes using RDT
		// if one sided (trend following), long term and be greedy <-- less and lesser throughout the day
		// if wave (find the turning points), short term and don't be greedy <-- more and more throughout the day
		// if flat, do nothing
		
		openTickets = new ArrayList<String>(3);
		tickMap = new HashMap<FXAsset, Tick>();
		for (FXAsset asset : FXAsset.values()) {
			declare(asset.toString(), Constants.MOCK_BROKER);
		}
	}

	@Override
	public boolean run(Object o) {
		if (o instanceof Tick) {
			Tick t = (Tick) o;
			if (t.getSpread() < 0) {
				final double depth = Math.abs(t.getAskDepth() - t.getBidDepth());
				// can be improved to check the trend, if it is uptrend, buy
				// first and set a stop lose to sell later
				String ticket = forex.send(t.getSymbol(), Type.MARKET, Side.BUY, depth, 0.0,
						0.0, Constants.MOCK_BROKER);
				openTickets.add(ticket);
				forex.send(t.getSymbol(), Type.MARKET, Side.SELL, depth, 0.0,
						0.0, Constants.MOCK_BROKER);
			}
		}
		return false;
	}
}
